export default {
  namespaced: true,
  state: {
    addressInfo: JSON.parse(uni.getStorageSync('addressInfo') || '{}'),
    token: uni.getStorageSync('token') || '',
    userInfo: JSON.parse(uni.getStorageSync('userInfo') || '{}'),
    redirectInfo: {
      openType: '',
      url: ''
    }
  },
  mutations: {
    updateAddressInfo(state, addressInfo) {
      state.addressInfo = addressInfo
      this.commit('user/saveToStorage')
    },
    saveToStorage(state) {
      uni.setStorageSync('addressInfo', JSON.stringify(state.addressInfo))
    },
    updateUserInfo: (state, userInfo) => {
      state.userInfo = userInfo
      uni.setStorageSync('userInfo', JSON.stringify(state.userInfo))
    },
    updateToken: (state, token) => {
      state.token = token
      uni.setStorageSync('token', state.token)
    },
    logout: state => {
      state.userInfo = {}
      state.token = ''
      state.addressInfo = {}
      uni.removeStorageSync('userInfo')
      uni.removeStorageSync('token')
      uni.removeStorageSync('addressInfo')
    },
    updateRedirectInfo: (state, redirectInfo) => state.redirectInfo = redirectInfo
  },
  actions: {
    updateAddressInfo(context, addressInfo) {
      context.commit('updateAddressInfo', addressInfo)
    },
    updateUserInfo: (context, userInfo) => context.commit('updateUserInfo', userInfo),
    updateToken: (context, token) => context.commit('updateToken', token),
    logout: context => context.commit('logout'),
    updateRedirectInfo: (context, redirectInfo) => context.commit('updateRedirectInfo', redirectInfo)
  },
  getters: {
    fullAddress(state) {
      const { cityName, countyName, detailInfo, provinceName } = state.addressInfo
      return (provinceName + cityName + countyName + detailInfo) || ''
    }
  }
}